import re 

from django.contrib.auth.models import User, UserManager
from django.utils.translation import ugettext, ugettext_lazy as _
from django.db import models

from multiprofiles.collector import ProfileCollector
import multiprofiles.utils as utils
import multiprofiles.signals

ProfileCollector.gatherinfo()


class ProfiledUser(utils.get_user_class(True)):
    """
    Model for user with different Profiles support.
    """
    class Meta:
        verbose_name = _('User')
        verbose_name_plural = _('Users')

    invoke_profile_change = models.BooleanField()
    profile_type = models.CharField(
                max_length=30, 
                choices=ProfileCollector.profiles_info, 
                default=ProfileCollector.profiles_info[0],
                help_text=_("User Profile type"),
                blank=False)
 
    objects = UserManager()
 
    # In the save function, we implement our own password
    # management. If the password is already hashed in the form
    # we just dont change anything otherwise we call the set_password()
    def save(self):
        password = ""
        r = re.compile('sha1\$.*')
        if not r.match(self.password):
            password = self.password
            self.set_password(self.password)
        User.save(self)


ProfileCollector.autodiscover()